package com.longrou.base.activity.main

import android.databinding.DataBindingUtil
import android.os.Bundle
import android.support.v4.app.FragmentTransaction
import android.support.v4.content.ContextCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.ashokvarma.bottomnavigation.BottomNavigationBar
import com.ashokvarma.bottomnavigation.BottomNavigationItem
import com.longrou.base.R
import com.longrou.base.base.BaseActivity
import com.longrou.base.base.BaseAppKT
import com.longrou.base.databinding.ActivityMainBinding
import com.longrou.base.fragment.HomeFragment

/**
 * @Description:
 * @Author: LongRou
 * @CreateDate: 2018-05-17 23:35
 * @Version: 1.0
 **/
@Route(path = "/Main/Main")
class MainActivity : BaseActivity(), BottomNavigationBar.OnTabSelectedListener {
    var binding : ActivityMainBinding? = null
    val waitTime :Long =2000
    var touchTime :Long = 0
    var homeFragment : HomeFragment = HomeFragment()
    val fragmentTag = arrayOf("Home","","","")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
        init()
    }
    fun init(){
        //initBottomBar
        binding!!.aMainBottomBar.setTabSelectedListener(this)
        binding!!.aMainBottomBar.setMode(BottomNavigationBar.MODE_FIXED)
        binding!!.aMainBottomBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC)
        binding!!.aMainBottomBar.setBackgroundResource(R.color.bg_floder_press)
        binding!!.aMainBottomBar
                .addItem(BottomNavigationItem(R.mipmap.ic_home, "首页").setActiveColorResource(R.color.AliXYHuang))
                .addItem(BottomNavigationItem(R.mipmap.ic_comment, "社区").setActiveColorResource(R.color.AliLan))
                .addItem(BottomNavigationItem(R.mipmap.ic_crown, "积分").setActiveColorResource(R.color.AliRou))
                .setFirstSelectedPosition(0)
                .initialise()
        setDefaultFragment()
    }
    fun setDefaultFragment(){
        setColoringStatusBar(ContextCompat.getColor(applicationContext,R.color.AliXYHuang))
        var transition = supportFragmentManager.beginTransaction()
        transition.add(binding!!.aMainLayout.id, homeFragment,fragmentTag[0])
        transition.show(homeFragment).commit()
        //supportFragmentManager.beginTransaction().replace(binding!!.aMainLayout.id, homeFragment).commit()
    }

    override fun onTabReselected(position: Int) {
    }

    override fun onTabUnselected(position: Int) {
    }

    override fun onTabSelected(position: Int) {
        var transition = supportFragmentManager.beginTransaction()
        hideAllFragment(transition)
        when(position){
            0 ->{
                setColoringStatusBar(ContextCompat.getColor(applicationContext,R.color.AliXYHuang))
                if(supportFragmentManager.findFragmentByTag(fragmentTag[0]) == null)transition.add(binding!!.aMainLayout.id, homeFragment,fragmentTag[0])
                transition.show(supportFragmentManager.findFragmentByTag(fragmentTag[0])).commit()
                //supportFragmentManager.beginTransaction().replace(binding!!.aMainLayout.id, homeFragment).commit()
            }
            1 ->{
                setColoringStatusBar(ContextCompat.getColor(applicationContext,R.color.AliLan))
            }
            2 ->{
                setColoringStatusBar(ContextCompat.getColor(applicationContext,R.color.AliRou))
//                if(supportFragmentManager.findFragmentByTag(fragmentTag[2]) == null)transition.add(binding!!.aMainLayout.id, memberFragment,fragmentTag[2])
//                transition.show(memberFragment).commit()
                //supportFragmentManager.beginTransaction().replace(binding!!.aMainLayout.id, memberFragment).commit()
            }
        }
    }

    fun hideAllFragment(transition : FragmentTransaction){
        if(supportFragmentManager.findFragmentByTag(fragmentTag[0]) != null)transition.hide(supportFragmentManager.findFragmentByTag(fragmentTag[0]))
        //if(supportFragmentManager.findFragmentByTag(fragmentTag[2]) != null)transition.hide(supportFragmentManager.findFragmentByTag(fragmentTag[2]))
    }

    override fun onBackPressed() {
        val currentTime = System.currentTimeMillis()
        if (currentTime - touchTime >= waitTime) {
            showToast("再按一次退出程序")
            touchTime = currentTime
        } else {
            (application as BaseAppKT).exitApp()
            //System.exit(0);
        }
    }
}